<template>
  <div class="app-container">
    <el-form :rules="formRules" ref="editForm" label-width="auto" :model="form" size="medium">
      <el-form-item label="原密码" prop="rawPwd">
        <el-input type="password" v-model="form.rawPwd" placeholder="请输入原密码"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPwd">
        <el-input type="password" v-model="form.newPwd" placeholder="请输入新密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPwd">
        <el-input type="password" v-model="form.confirmPwd" placeholder="请再次输入密码"></el-input>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="onSubmitChange" :loading="loading">{{ loading ? '提交中 ...' : '确 定' }}</el-button>
  </div>
</template>

<script>

import {changePassword} from "@/api/user";
import UserValidator from "@/validator/UserValidator";

export default {
  data() {
    return {
      form: {
        rawPwd: '',
        newPwd: '',
        confirmPwd: '',
      },
      formRules: {
        ...UserValidator.changePwdRules
      },
      loading: false
    }
  },
  methods: {
    onSubmitChange() {
      if(this.form.rawPwd === this.form.newPwd) {
        this.$message({
          type: 'error',
          message: '原密码不能和新密码相同'
        })
        return
      }
      if(this.form.newPwd !== this.form.confirmPwd) {
        this.$message({
          type: 'error',
          message: '密码输入不一致，请重新输入'
        })
        return
      }
      const form = {
        rawPwd: this.form.rawPwd,
        newPwd: this.form.newPwd,
      }
      this.loading = true
      changePassword(form).then(res => {
        this.$message({
          type: 'success',
          message: res.msg
        })
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
